﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomationHost.ViewModels;
using System.Text;

namespace AutomationHost.Areas.Run.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["HisoftSmartEntities"].ConnectionString;
            
        public ActionResult Index()
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                RunIndexPage model = new RunIndexPage();
                model.TestPlans = db.TestPlans.ToList();
                model.Devices = db.Devices.ToList();

                return View(model);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)] 
        public ActionResult Index(FormCollection form)
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                int planID = int.Parse(form[0]);
                string deviceGUID = form[1];
                StringBuilder caseRunStates = new StringBuilder();
                foreach (var testCaseID in db.TestPlans.First(p => p.ID == planID).CaseIDs.Split(';'))
                {
                    if (!string.IsNullOrEmpty(testCaseID))
                    {
                        caseRunStates.Append(string.Format("{0},0;", testCaseID));
                    }
                }

                TestRun testRun = new TestRun()
                {
                    DeviceGUID = deviceGUID,
                    TestPlanID = planID,
                    RunTime = DateTime.UtcNow,
                    Executor = "Leo",
                    State = "Pending",
                    CaseRunStates = caseRunStates.ToString()
                };

                db.TestRuns.AddObject(testRun);
                db.SaveChanges();
            }

            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                RunListPage model = new RunListPage();
                model.TestRuns = new List<TestRun>();
                foreach (var run in db.TestRuns.ToList())
                {
                    run.TestPlanModel = db.TestPlans.First(p => p.ID == run.TestPlanID);
                    run.Device = new Device()
                    {
                        IP = run.Device.IP,
                        OS = run.Device.OS,
                        OSVersion = run.Device.OSVersion,
                        Name = run.Device.Name,
                        GUID = run.DeviceGUID
                    };
                    model.TestRuns.Add(run);
                }

                return View(model);
            }
        }

        public ActionResult Rerun(int id)
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                var testRun = db.TestRuns.First(r => r.ID == id);
                testRun.State = "Running";
                StringBuilder caseRunStates = new StringBuilder();
                foreach (var testCaseID in db.TestPlans.First(p => p.ID == testRun.TestPlanID).CaseIDs.Split(';'))
                {
                    if (!string.IsNullOrEmpty(testCaseID))
                    {
                        caseRunStates.Append(string.Format("{0},0;", testCaseID));
                    }
                }

                testRun.CaseRunStates = caseRunStates.ToString();
                db.SaveChanges();
            }

            return RedirectToAction("List");
        }
    }
}
